package com.ziytek.web.citizen.job;

import com.ziytek.web.citizen.PortCheckerUtil;
import com.ziytek.web.citizen.ReBootService;
import com.ziytek.web.citizen.ServerConstant;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;


/**
 * @author fenghx
 * @version v1.0
 * @className RunningJob
 * @descripton 程序正在运行中
 * @date 2023/4/11 20:07
 **/
@Slf4j
@Service
public class ServiceRunCheck {

    @Resource
    private ReBootService rebootService;

    @PostConstruct
    public void estimatedOptimalThreadCountInit() {
        log.info("estimatedOptimalThreadCountInit");
    }

    @Scheduled(fixedRate = 60000)
    public void serviceRunningCheck() {
        // 程序正在运行中
        //log.info("serviceRunningCheck start");

        // 使用端口占用情况,检查程序是否运行
        if (PortCheckerUtil.isPortInUse(ServerConstant.PORT_TCP) && PortCheckerUtil.isPortInUse(ServerConstant.PORT_SERVER)) {
            //log.info("程序已运行,请勿重复启动!");
            // 弹出窗口 提示
            //JOptionPane.showMessageDialog(null, "程序已运行,请勿重复启动!");
            return;
        }

        log.info("(重启程序)程序未运行,可以正常启动!");
        //rebootService.rebootRun();
        rebootService.rebootRunExe();
    }
}
